<?php
namespace ryan;

class Rsa
{
    /**
     * rsa加密
     * @access protected
     * @param  string  $data 数据
     * @return base64
     */
    public function encryption($data) {
        $file = fopen(__KEY__ . '/pub.key', "r");
        $pubKey = fread($file, filesize(__KEY__ . '/pub.key'));
        fclose($file);
        $result = openssl_pkey_get_public($pubKey);
        openssl_public_encrypt($data, $encryption, $result);
        $encryption = base64_encode($encryption);
        return $encryption;
    }

    /**
     * rsa解密
     * @access protected
     * @param  string  $data 数据
     * @return string
     */
    public function decryption($data) {
        $file = fopen(__KEY__ . '/pri.key', "r");
        $priKey = fread($file, filesize(__KEY__ . '/pri.key'));
        fclose($file);
        $result = openssl_pkey_get_private($priKey);
        openssl_private_decrypt(base64_decode($data), $decryption, $result);
        return $decryption;
    }
}